package kd.bos.openapi.demo.auth.digestauth;

import com.alibaba.fastjson.JSON;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.openapi.demo.auth.signauth.dto.BosKeyInfoDto;
import kd.bos.openapi.demo.auth.signauth.dto.ThirdAppInfoDto;
import kd.bos.openapi.sdk.auth.digestauth.DigestParamDto;
import kd.bos.openapi.sdk.auth.digestauth.OpenApiDigestAuthUtil;
import kd.bos.openapi.sdk.dto.OrderDto;
import kd.bos.openapi.sdk.request.RequestOperatorDto;
import kd.bos.openapi.sdk.response.query.QueryDataDto;
import kd.bos.openapi.sdk.response.save.SaveDataDto;
import kd.bos.openapi.sdk.util.PropertyUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author johnz
 * @Deacription 摘要认证测试用例，这里测试用例都采用新的摘要认证方式，即通过openApiSign参数替换appId, user,userType,accountId,且密文传输，更高效安全。推荐这种方式。
 * @date 2023年10月23日
 */
public class DigestDemoTest {
    private final static Logger log = Logger.getLogger(DigestDemoTest.class.getName());
    //苍穹OpenAPI第三方应用-基本认证策略-Secret key
    public static final String openApiSign = PropertyUtil.getProperty("digestAuth.openApiSign");
    //苍穹OpenAPI第三方应用-摘要认证策略-认证密钥
    public static final String appSecret = PropertyUtil.getProperty("digestAuth.appSecret");
    // 苍穹OpenAPI调用接口的URL前缀
    public static final String urlPrefix = PropertyUtil.getProperty("bos.env.cosmic");
    /**
     * 接口测试主方法，测试之前，需要将三个参数改为所在环境的地址，还要确保对应的苍穹系统有对应的API接口，入参和出参信息。配置好后，可以根据业务场景需要，取消对应的注释即可测试。
     * 相关的依赖包已整理到lib库中，工程需要设置依赖库lib。
     * 如果需要打印请求参数，请将DigestParamDto.isPrint参数设置为true
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
           testDigestGet();
        // testDigestPostFindOrders();
        // testSignOperatorSaveApi();
        // testSignCustomApi();
    }

    /**
     * 测试GET请求，摘要认证
     *
     * @throws Exception
     */
    public static void testDigestGet() throws Exception {
        log.info("------------------------testDigestGet start");
        // 接口地址
        StringBuilder url = new StringBuilder(urlPrefix + "/kapi/v2/kdtest/open/openapi_unittest/findOrdersByGet");
        // GET接口需要签名的参数和值
        String content = "billno=1&pageSize=10&pageNo=1";
        // GET接口需要签名的参数，多参数逗号分开
        String parameters = "billno,pageSize,pageNo";
        // 构造摘要认证需要的参数
        DigestParamDto params = new DigestParamDto("2", appSecret, openApiSign, parameters);
        // 打印请求参数
        params.setPrint(true);
        // 发送请求
        CustomApiResult<QueryDataDto> result = OpenApiDigestAuthUtil.sendDigestGetApiRequest(url.toString(), params, QueryDataDto.class, content);
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testDigestGet end");
    }

    /**
     * 测试post请求，摘要认证
     *
     * @throws Exception
     */
    public static void testDigestPostFindOrders() throws Exception {
        log.info("------------------------testDigestNewPostFindOrders start");
        // 接口地址
        String url = urlPrefix + "/kapi/v2/kdtest/open/openapi_unittest/findOrders";
        // 请求body参数
        String content = "{\n" +
                "\t\"data\":{\n" +
                "\t\t\"billno\":\"003\",\n" +
                "\t\t\"billstatus\":\"A\"\n" +
                "\t},\n" +
                "\t\"pageSize\":10,\n" +
                "\t\"pageNo\":1\n" +
                "}";
        // 构造摘要认证需要的参数
        DigestParamDto params = new DigestParamDto("2", appSecret, openApiSign, null);
        // 发送请求
        CustomApiResult<QueryDataDto> result = OpenApiDigestAuthUtil.sendDigestApiRequest(url.toString(), params, QueryDataDto.class, content);
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testDigestPostFindOrdersByNew end");
    }


    /**
     * 测试操作类API 保存操作 摘要认证
     */
    public static void testSignOperatorSaveApi() throws Exception {
        log.info("------------------------testSignOperatorSaveApi start");
        // 接口地址
        String url = "/kapi/v2/kdtest/open/openapi_unittest/signAuth_save";
        // 请求模型封装
        RequestOperatorDto<OrderDto> openApiRequest = new RequestOperatorDto<>();
        OrderDto orderDto = new OrderDto();
        orderDto.setBillno("unittest-00000003");
        openApiRequest.setData(orderDto);
        // 构造摘要认证需要的参数, 这里的OrderDto对象中的属性对应API接口界面的入参定义
        DigestParamDto params = new DigestParamDto("2", appSecret, openApiSign, null);
        // 发送请求
        CustomApiResult<SaveDataDto> result = OpenApiDigestAuthUtil.sendDigestApiRequest(urlPrefix + url, params, SaveDataDto.class, JSON.toJSONString(openApiRequest));
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testSignOperatorSaveApi end");
    }

    /**
     * 测试自定义API 摘要认证
     */
    public static void testSignCustomApi() throws Exception {
        log.info("------------------------testSignCustomApi start");
        // 接口地址
        String url = "/kapi/v2/open/openapi/gen/getBosRsaPublicKey";
        // 请求模型封装，这里的thirdAppInfoDto对象中的属性对应接口界面入参的定义，自定义API接口入参需要用map将构建的对象包起来，名称为ThirdAppInfoDto对象名称小驼峰形式thirdAppInfoDto
        ThirdAppInfoDto thirdAppInfoDto = new ThirdAppInfoDto();
        thirdAppInfoDto.setThirdId("1533186420334907392");
        thirdAppInfoDto.setAccountId("1173910536060928000");
        Map<String, ThirdAppInfoDto> requestParams = new HashMap<>();
        requestParams.put("thirdAppInfoDto", thirdAppInfoDto);
        // 发送请求
        DigestParamDto params = new DigestParamDto("2", appSecret, openApiSign, null);
        // 发送请求
        CustomApiResult<BosKeyInfoDto> result = OpenApiDigestAuthUtil.sendDigestApiRequest(urlPrefix + url, params, BosKeyInfoDto.class, JSON.toJSONString(thirdAppInfoDto));
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testSignCustomApi end");
    }

}
